package com.ruoyi.plus.pub.api;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.api.ApiResult;
import com.ruoyi.common.core.api.BaseApi;
import com.ruoyi.common.core.api.IdsEntity;
import com.ruoyi.common.core.api.version.ApiVersion;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.plus.pub.api.po.PubCommonWordsPo;
import com.ruoyi.plus.pub.api.po.PubCommonWordsQueryPo;
import com.ruoyi.plus.pub.api.vo.PubCommonWordsVo;
import com.ruoyi.plus.pub.domain.PubCommonWords;
import com.ruoyi.plus.pub.service.IPubCommonWordsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 常用语Api
 *
 */
@Slf4j
@Api(tags = "PubCommonWordsApi-常用语接口")
@ApiVersion()
@RestController
@RequestMapping("app/api/pub/pubCommonWordsApi")
public class PubCommonWordsApi extends BaseApi {

    @Autowired
    private IPubCommonWordsService pubCommonWordsService;

    /**
     * 获取常用语对象
     */
    @ApiOperation("get-获取常用语对象")
    @GetMapping("/get/{id}")
    @ResponseBody
    public ApiResult<PubCommonWordsVo> get(@PathVariable("id") Long id) {
        PubCommonWords commonWords = pubCommonWordsService.getById(id);
        if (commonWords == null || !commonWords.getUserId().equals(SecurityUtils.getUserId())) {
            return new ApiResult<PubCommonWordsVo>(this.getApiVersion()).setCode(-1).setMsg("所修改的数据不是本用户的数据!");
        }
        return buildGetApiResult(id, commonWords, PubCommonWordsVo.class).setApiVersion(this.getApiVersion());
    }

    /**
     * 查询常用语列表
     */
    @ApiOperation("list-获取常用语列表")
    @PostMapping("/list")
    @ResponseBody
    public ApiResult<PageInfo<PubCommonWordsVo>> list(@Validated @RequestBody PubCommonWordsQueryPo pubCommonWordsQueryPo) {
        PubCommonWords pubCommonWords = this.buildCopyClass(pubCommonWordsQueryPo, PubCommonWords.class);
        pubCommonWords.setUserId(SecurityUtils.getUserId());
        PageHelper.startPage(pubCommonWordsQueryPo.getPageNum(), pubCommonWordsQueryPo.getPageSize());
        List<PubCommonWords> list = pubCommonWordsService.selectPubCommonWordsList(pubCommonWords);
        return buildListApiResult(list, PubCommonWordsVo.class).setApiVersion(this.getApiVersion());
    }

    /**
     * 新增保存常用语
     */
    @ApiOperation("新增保存常用语")
    @Log(title = "add-常用语", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public ApiResult<Boolean> addSave(@Validated @RequestBody PubCommonWordsPo pubCommonWordsPo) {
        PubCommonWords pubCommonWords = buildCopyClass(pubCommonWordsPo, PubCommonWords.class);
        pubCommonWords.setUserId(SecurityUtils.getUserId());
        pubCommonWords.setNickName(SecurityUtils.getLoginUser().getUser().getNickName());
        return new ApiResult<Boolean>().setData(pubCommonWordsService.save(pubCommonWords)).setApiVersion(this.getApiVersion());
    }

    /**
     * 修改保存常用语
     */
    @ApiOperation("修改保存常用语")
    @Log(title = "常用语", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public ApiResult<Boolean> editSave(@Validated @RequestBody PubCommonWordsPo pubCommonWordsPo) {
        if (pubCommonWordsPo.getId() == null) {
            return new ApiResult<Boolean>(this.getApiVersion()).setCode(-2).setMsg("修改时id字段为必填项!");
        }
        PubCommonWords commonWords = pubCommonWordsService.getById(pubCommonWordsPo.getId());
        if (commonWords == null || !commonWords.getUserId().equals(SecurityUtils.getUserId())) {
            return new ApiResult<Boolean>(this.getApiVersion()).setCode(-1).setMsg("所修改的数据不是本用户的数据!");
        }

        PubCommonWords pubCommonWords = buildCopyClass(pubCommonWordsPo, PubCommonWords.class);
        pubCommonWords.setUserId(SecurityUtils.getUserId());
        return new ApiResult<Boolean>().setData(pubCommonWordsService.saveOrUpdate(pubCommonWords)).setApiVersion(this.getApiVersion());
    }

    /**
     * 删除常用语
     */
    @ApiOperation("remove-删除常用语")
    @Log(title = "常用语", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public ApiResult<Boolean> remove(@RequestBody IdsEntity idsEntity) {
        Long[] ids = Convert.toLongArray(idsEntity.getIds());
        for (Long id : ids) {
            PubCommonWords commonWords = pubCommonWordsService.getById(id);
            if (commonWords == null || !commonWords.getUserId().equals(SecurityUtils.getUserId())) {
                return new ApiResult<Boolean>(this.getApiVersion()).setCode(-1).setMsg("所删除的数据不是本用户数据!");
            }
        }
        return new ApiResult<Boolean>().setData(pubCommonWordsService.removeByIds(Arrays.asList(ids))).setApiVersion(this.getApiVersion());
    }

}
